package com.freesky.biz.activity.service.impl;

import com.freesky.biz.activity.domain.*;
import com.freesky.biz.activity.mapper.AlipayBillTaskMapper;
import com.freesky.biz.activity.service.IAlipayBillTasklService;
import com.freesky.biz.activity.service.ICouponBillService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AlipayBillTaskServiceImpl implements IAlipayBillTasklService {
    @Autowired
    private AlipayBillTaskMapper alipayBillTaskMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TAlipayBill> selectBillList(TAlipayBill entity) {
        return alipayBillTaskMapper.selectBillList(entity);
    }

    @Override
    public AjaxResult exportFile(TAlipayBill entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("ALIPAY_BILL");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TAlipayBill(), IAlipayBillTasklService.class,
                "selectBillList", "核销明细（支付宝）",Long.valueOf(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public TAlipayActivityConfigExport selectBillSummaryData(TAlipayBill entity) {
        return alipayBillTaskMapper.selectBillSummaryData(entity);
    }

    @Override
    public TAlipayActivityConfigExport selectRefundBillSummaryData(TAlipayRefundBill entity) {
        return alipayBillTaskMapper.selectRefundBillSummaryData(entity);
    }

    @Override
    public List<TAlipayBillSummary> selectBillSummaryList(TAlipayBill entity) {
        return alipayBillTaskMapper.selectBillSummaryList(entity);
    }

    @Override
    public AjaxResult exportSummaryFile(TAlipayBill entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("ALIPAY_BILL_SUMMARY");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TAlipayBillSummary(), IAlipayBillTasklService.class,
                "selectBillSummaryList", "核销明细汇总",Long.valueOf(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public List<TAlipayRefundBill> selectRefundBillList(TAlipayRefundBill entity) {
        return alipayBillTaskMapper.selectRefundBillList(entity);
    }

    @Override
    public AjaxResult exportRefundFile(TAlipayRefundBill entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("ALIPAY_REFUND_BILL");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TAlipayRefundBill(), IAlipayBillTasklService.class,
                "selectRefundBillList", "退款明细（支付宝）",Long.valueOf(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

}
